package com.jsbs.iam.user.controller;

import com.alibaba.fastjson.JSON;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.user.param.*;
import com.jsbs.iam.user.service.OrgInfoService;
import com.jsbs.iam.user.service.PostInfoService;
import com.jsbs.iam.user.service.PostOrgRelationService;
import com.jsbs.iam.user.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @Author junwc
 * @Date 2022/12/8
 * @Version 1.0
 */
@RestController
public class UserInfoApiController extends BaseController{
    private static final Logger logger = LoggerFactory.getLogger(UserInfoApiController.class);

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private PostInfoService postInfoService;

    @Autowired
    private PostOrgRelationService postOrgRelationService;

    /**
     * 功能描述：人资同步人员主数据
     * @param userInfoParam
     * @return
     */
    @PostMapping("/api/user/info/sync")
    public Result syncUserInfo(@RequestBody UserInfoParam userInfoParam) {
        logger.info("Route==>UserInfoApiController method=syncUserInfo 人资同步人员主数据开始，入参：{}", JSON.toJSONString(userInfoParam));
        try {
            if (Objects.isNull(userInfoParam.getDataSource())) {
                logger.info("Route==>UserInfoApiController method=syncUserInfo 人资同步人员主数据组织类型为空:{}", JSON.toJSONString(userInfoParam));
                return Result.failWithMsg("组织来源为空");
            }
            if (Objects.isNull(userInfoParam.getCompanyCode())) {
                logger.info("Route==>UserInfoApiController method=syncUserInfo 人资同步人员主数据归属租户类型为空:{}", JSON.toJSONString(userInfoParam));
                return Result.failWithMsg("归属租户为空");
            }
            if (Objects.isNull(userInfoParam.getUserTypeCode())) {
                logger.info("Route==>UserInfoApiController method=syncUserInfo 人资同步人员主数据归属用户类型为空:{}", JSON.toJSONString(userInfoParam));
                return Result.failWithMsg("用户类型为空");
            }
            List<UserInfoResp> respList = userInfoService.syncUserInfo(userInfoParam);
            return Result.success(respList);
        } catch (Exception e) {
            logger.error("Route==>UserInfoApiController method=syncUserInfo 人资同步人员主数据异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：人资同步组织架构主数据
     * @param orgInfoParam
     * @return
     */
    @PostMapping("/api/org/info/sync")
    public Result syncOrgInfo(@RequestBody OrgInfoParam orgInfoParam) {
        logger.info("Route==>UserInfoApiController method=syncOrgInfo 人资同步组织架构主数据开始，入参：{}", JSON.toJSONString(orgInfoParam));
        try {
            if (Objects.isNull(orgInfoParam.getDataSource())) {
                logger.info("Route==>UserInfoApiController method=syncOrgInfo 人资同步组织架构主数据组织类型为空:{}", JSON.toJSONString(orgInfoParam));
                return Result.failWithMsg("组织来源为空");
            }
            if (Objects.isNull(orgInfoParam.getCompanyCode())) {
                logger.info("Route==>UserInfoApiController method=syncOrgInfo 人资同步组织架构主数据归属租户为空:{}", JSON.toJSONString(orgInfoParam));
                return Result.failWithMsg("归属租户为空");
            }
            orgInfoService.syncOrgInfo(orgInfoParam);
            return Result.success();
        } catch (Exception e) {
            logger.error("Route==>UserInfoApiController method=syncOrgInfo 人资同步组织架构主数据异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：人资同步岗位主数据
     * @param postInfoParam
     * @return
     */
    @PostMapping("/api/post/info/sync")
    public Result syncPostInfo(@RequestBody PostInfoParam postInfoParam) {
        logger.info("Route==>UserInfoApiController method=syncPostInfo 人资同步岗位主数据开始，入参：{}", JSON.toJSONString(postInfoParam));
        try {
            if (Objects.isNull(postInfoParam.getDataSource())) {
                logger.info("Route==>UserInfoApiController method=syncPostInfo 人资同步岗位主数据组织类型为空:{}", JSON.toJSONString(postInfoParam));
                return Result.failWithMsg("组织来源为空");
            }
            if (Objects.isNull(postInfoParam.getCompanyCode())) {
                logger.info("Route==>UserInfoApiController method=syncPostInfo 人资同步岗位主数据归属租户为空:{}", JSON.toJSONString(postInfoParam));
                return Result.failWithMsg("归属租户为空");
            }
            postInfoService.syncPostInfo(postInfoParam);
            return Result.success();
        } catch (Exception e) {
            logger.error("Route==>UserInfoApiController method=syncPostInfo 人资同步岗位主数据异常：", e);
            return handleException(e);
        }
    }

    /**
     * 功能描述：人资同步岗位与组织关系主数据
     * @param postOrgParam
     * @return
     */
    @PostMapping("/api/post/org/sync")
    public Result syncPostOrgInfo(@RequestBody PostOrgParam postOrgParam) {
        logger.info("Route==>UserInfoApiController method=syncPostOrgInfo 人资同步岗位与组织关系主数据开始，入参：{}", JSON.toJSONString(postOrgParam));
        try {
            if (Objects.isNull(postOrgParam.getDataSource())) {
                logger.info("Route==>UserInfoApiController method=syncPostOrgInfo 人资同步岗位与组织关系主数据组织类型为空:{}", JSON.toJSONString(postOrgParam));
                return Result.failWithMsg("组织来源为空");
            }
            if (Objects.isNull(postOrgParam.getCompanyCode())) {
                logger.info("Route==>UserInfoApiController method=syncPostOrgInfo 人资同步岗位与组织关系主数据归属租户为空:{}", JSON.toJSONString(postOrgParam));
                return Result.failWithMsg("归属租户为空");
            }
            postOrgRelationService.syncPostOrgInfo(postOrgParam);
            return Result.success();
        } catch (Exception e) {
            logger.error("Route==>UserInfoApiController method=syncPostOrgInfo 人资同步岗位与组织关系主数据异常：", e);
            return handleException(e);
        }
    }



}
